package com.dmwork.service.work.controller.front.me;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dmwork.common.base.result.R;
import com.dmwork.service.base.model.VideoListVo;
import com.dmwork.service.work.entity.Work;
import com.dmwork.service.work.entity.WorkVideoInfo;
import com.dmwork.service.work.feign.VodVideoFeignService;
import com.dmwork.service.work.service.WorkService;
import com.dmwork.service.work.service.WorkVideoInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 李天
 * @since 2021-03-14
 */
@Slf4j
@Api(description = "作品展示视频管理")
@Transactional
@RestController
@RequestMapping("/front/dm_work/work/work_video/me")
public class FrontMeWorkVideoInfoController {

    @Autowired
    private WorkVideoInfoService workVideoInfoService;
    @Autowired
    private WorkService workService;
    @Autowired
    private VodVideoFeignService vodVideoFeignService;

    @ApiOperation("添加视频信息")
    @PostMapping("add_info")
    public R addInfo(@ApiParam(value = "添加视频提交信息", required = true) @RequestBody WorkVideoInfo workVideoInfo) {
        workVideoInfoService.save(workVideoInfo);
        Work work = workService.getById(workVideoInfo.getWorkId());
        work.setVideoCount(work.getVideoCount() + 1);
        workService.updateById(work);
        return R.ok().message("添加成功");
    }

    @ApiOperation("修改指定视频信息")
    @PutMapping("update_info")
    public R updateInfo(@ApiParam(value = "视频信息", required = true) @RequestBody WorkVideoInfo workVideoInfo) {
        workVideoInfoService.updateById(workVideoInfo);
        return R.ok().message("修改成功");
    }

    @ApiOperation("获取指定作品的视频信息")
    @GetMapping("get_info_list/{workId}")
    public R getInfoList(@ApiParam(value = "作品id", required = true) @PathVariable("workId") String workId) {
        QueryWrapper<WorkVideoInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("work_id", workId);
        List<WorkVideoInfo> workVideoInfoList = workVideoInfoService.list(queryWrapper);
        return R.ok().data("items", workVideoInfoList);
    }

    @ApiOperation("删除指定视频信息")
    @DeleteMapping("remove/{videoId}")
    public R remove(@PathVariable("videoId") String videoId) {

        WorkVideoInfo workVideoInfo = workVideoInfoService.getById(videoId);
        //获取作品id
        String workId = workVideoInfo.getWorkId();
        //获取视频资源id
        String videoSourceId = workVideoInfo.getVideoSourceId();

        //指定作品的video_count-1
        Work work = workService.getById(workId);
        work.setVideoCount(work.getVideoCount() - 1);
        workService.updateById(work);
        //删除数据库中的视频信息
        workVideoInfoService.removeById(videoId);

        //远程调用VOD微服务删除视频资源
        vodVideoFeignService.removeVideo(videoSourceId);

        return R.ok().message("删除成功");
    }

    @ApiOperation("批量删除视频信息")
    @DeleteMapping("batch_remove/{workId}")
    public R batchRemove(@PathVariable String workId, @RequestBody List<WorkVideoInfo> workVideoInfoList) {
        List<String> idList = new ArrayList<>();
        List<String> videoSourceIdList = new ArrayList<>();
        for (WorkVideoInfo workVideoInfo : workVideoInfoList) {
            idList.add(workVideoInfo.getId());
            videoSourceIdList.add(workVideoInfo.getVideoSourceId());
        }

        log.info(Arrays.toString(idList.toArray()));
        log.info(Arrays.toString(videoSourceIdList.toArray()));

        //删除数据库视频信息
        workVideoInfoService.removeVideoInfoByIds(workId, idList);
        //删除vod视频信息
        new Thread(() -> {
            vodVideoFeignService.removeVideoByIdList(VideoListVo.builder().videoIdList(videoSourceIdList).build());
        }).start();
        return R.ok().message("删除成功");
    }

}

